<?php
class JSSDK {
	private $appId;
	private $appSecret;
	public function __construct($appId, $appSecret) {
		$this->appId = $appId;
		$this->appSecret = $appSecret;
	}
	public function getwxconfig($url, $callback) {
		header ( 'Access-Control-Allow-Origin:*' );
		/* if (! $vendor_mp = Cache::store ( $this->GetCache () )->get ( 'osman_vendor_mp2' )) {
			$vendors = Db::name ( 'vendor' )->where ( 'status', 8 )->find ();
			$vendor_mp = Db::name ( 'auth_account' )->where ( 'appid', $vendors ['accessKeyId'] )->find ();
			cache::store ( $this->GetCache () )->set ( 'osman_vendor_mp2', $vendor_mp, 600 );
		} */
		$vendor_mp = array();
		$jsapiTicket = $this->getJsApiTicket ( $vendor_mp );
		$timestamp = time ();
		$nonceStr = $this->createNonceStr( 16 );
		$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
		$signature = sha1 ( $string );
		$config = array (
				"appId" => $vendor_mp ['appid'],
				"nonceStr" => $nonceStr,
				"timestamp" => $timestamp,
				"signature" => $signature 
		);
		return jsonp ( $config );
	}
	public function get_component_access_token($appid, $com_appid, $com_appsecret, $component_verify_ticket) {
		//$component_access_token = cache::store ( $this->GetCache () )->get ( "2component_access_" . md5 ( $appid ) );
		$component_access_token = null;
		if (! $component_access_token) {			
			$data = '{"component_appid":"' . $com_appid . '","component_appsecret":"' . $com_appsecret . '","component_verify_ticket":"' . $component_verify_ticket . '"}';
			$url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
			$res = json_decode ( $this->curl_post ( $data, $url ) );
			$component_access_token = $res->component_access_token;			
		   //cache::store ( $this->GetCache () )->set ( '2component_access_' . md5 ( $appid ), $component_access_token, 580 );
		}
		return $component_access_token;
	}
	private function getJsApiTicket($vendor_mp) {	
		//$ticket = cache::store ( $this->GetCache () )->get ( "2osman_ticket_news_" . md5 ( $vendor_mp ['appid'] ) );
		$ticket = null;
		if (! $ticket) {
			$accessToken = $this->getAccessToken ( $vendor_mp );
			$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
			$res = file_get_contents ( $url );
			$data = json_decode ( $res );
			$ticket = $data->ticket;
			//cache::store ( $this->GetCache () )->set ( "2osman_ticket_news_" . md5 ( $vendor_mp ['appid'] ), $ticket, 7200 );			
		}
		return $ticket;
	}
	private function getAccessToken($vendor_mp) {
		//$cache_token = cache::store ( $this->GetCache () )->get ( "2osman_token_news_" . md5 ( $vendor_mp ['appid'] ));
		$cache_token = null;
		if (! $token = $cache_token) {			
			$com_access_token = $this->get_component_access_token ( $vendor_mp ['appid'], $vendor_mp ['com_appid'], $vendor_mp ['component_appsecret'], $vendor_mp ['ComponentVerifyTicket'] );			
			$url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=' . $com_access_token;
			$data = '{"component_appid":"' . $vendor_mp ['com_appid'] . '","authorizer_appid":"' . $vendor_mp ['appid'] . '","authorizer_refresh_token":"' . $vendor_mp ['authorizer_refresh_token'] . '"}';
			$res = $this->curl_post ( $data, $url );
			$data = json_decode ( $res );
			$token = $data->authorizer_access_token;
			//cache::store ( $this->GetCache () )->set ( "2osman_token_news_" . md5 ( $vendor_mp ['appid'] ), $token, 7200 );
		}
		return $token;
	}
	private function createNonceStr($length = 16) {
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		$str = "";
		for ($i = 0; $i < $length; $i++) {
			$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
		}
		return $str;
	}
	function curl_post($data, $url) {
		$curl = curl_init ();
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
		curl_setopt ( $curl, CURLOPT_TIMEOUT, 500 );
		curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );
		curl_setopt ( $curl, CURLOPT_URL, $url );
		curl_setopt ( $curl, CURLOPT_CUSTOMREQUEST, "POST" );
		curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );
		$res = curl_exec ( $curl );
		curl_close ( $curl );
		return $res;
	}
}
